﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 泛型
{
    class Program
    {
        static void Main(string[] args)
        {
            //泛型具体：类型安全
            BTreeNode<string> root =new BTreeNode<string>("北京");
            root.Left = new BTreeNode<string>("石家庄");
            root.Rigth = new BTreeNode<string>("济南");
            root.Left.Left = new BTreeNode<string>("太原");
            root.Left.Rigth = new BTreeNode<string>("济南");
            root.Rigth.Left = new BTreeNode<string>("郑州");
            root.Rigth.Rigth = new BTreeNode<string>("徐州");
            root.Left.Left.Left = new BTreeNode<string>("西安");
            root.Left.Rigth.Left = new BTreeNode<string>("武汉");
            Console.WriteLine("前序遍历：");
            root.PreorderTravese();
            Console.WriteLine("中序遍历：");
            root.InorderTravese();
            Console.WriteLine("后序遍历：");
            root.PosorderTravese();
            BTreeNode<int> root2 = new BTreeNode<int>(1);
            root2.Left = new BTreeNode<int>(-1);
            root2.Rigth = new BTreeNode<int>(2);
            root2.Rigth.Left= new BTreeNode<int>(4);
            root2.Rigth.Rigth = new BTreeNode<int>(5);
            Console.WriteLine("前序遍历：");
            root2.PreorderTravese();
            Console.WriteLine("中序遍历：");
            root2.InorderTravese();
            Console.WriteLine("后序遍历：");
            root2.PosorderTravese();
            Console.ReadKey();
        }
    }
    public class BTreeNode<T>
    {
        private T data;

        public T Data
        {
            get { return data; }
            set { if (!data.Equals(value))//T类型的数据不能用==
                    data = value; }
        }
        //递归定义
        public BTreeNode<T> Left { get; set; }
        public BTreeNode<T> Rigth { get; set; }
        public BTreeNode(T data)
        {
            this.data = data;
        }
        public void PreorderTravese()
        { 
            Console.WriteLine(this.data.ToString()+' ');
            if (this.Left != null)
                this.Left.PreorderTravese();
            if (this.Rigth != null)
                this.Rigth.PreorderTravese();
        }
        public void InorderTravese()
        {
            
            if (this.Left != null)
                this.Left.InorderTravese();
            Console.WriteLine(this.data.ToString() + ' ');
            if (this.Rigth != null)
                this.Rigth.InorderTravese();
        }
        public void PosorderTravese()
        {

            if (this.Left != null)
                this.Left.PosorderTravese();
           
            if (this.Rigth != null)
                this.Rigth.PosorderTravese();
            Console.WriteLine(this.data.ToString() + ' ');
        }
        //获取指定第N层的右子树
        public static BTreeNode<T> operator >>(BTreeNode<T> node, int n)
        {
            for (int i = 0; i < n && node.Rigth != null; i++)
            { node = node.Rigth; }
            return node;
        }
        //获取指定第N层的左子树
        public static BTreeNode<T> operator <<(BTreeNode<T> node, int n)//操作符重载必须用static
        {
            for (int i = 0; i < n && node.Rigth != null; i++)
            { node = node.Left; }
            return node;
        }
        public override string ToString()
        {
            return base.ToString();
        }
    }
}
